"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 目录不存在	双机场景：主机或备机dn1目录文件被删除。备机重建；备机平滑切换
Create At   : 2020/07/02
@zou_jialiang0505328114
Description :
    步骤 1.启动数据库
    步骤 2.删除主机dn1目录
    步骤 3.备机重建
    步骤 4.恢复主机dn1目录
    步骤 5.备机重建
    步骤 6.kill掉备机
    步骤 7.删除主机dn1目录
    步骤 8.备机重建
    步骤 9.恢复主机dn1目录
    步骤 10.主备重启
    步骤 11.删除备机dn1目录
    步骤 12.备机重建
    步骤 13.恢复备机dn1目录
    步骤 14.主备重启
    步骤 15.kill掉备机
    步骤 16.删除备机dn1目录
    步骤 17.备机重建
    步骤 18.恢复备机dn1目录
    步骤 19.主备重启
Expect      :
    步骤 1.成功
    步骤 2.成功
    步骤 3.fail
    步骤 4.成功
    步骤 5.成功
    步骤 6.成功
    步骤 7.成功
    步骤 8.fail
    步骤 9.成功
    步骤 10.成功
    步骤 11.成功
    步骤 12.fail
    步骤 13.成功
    步骤 14.成功
    步骤 15.成功
    步骤 16.成功
    步骤 17.fail
    步骤 18.成功
    步骤 19.成功
History     :
    Modified by @wan005 2021/7/19: 步骤10，start前增加状态查询和stop步骤
"""

import unittest
import time
import os
import queue
from yat.test import macro
from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *


class Deletaduit(unittest.TestCase):
    sh_primy = CommonSH('PrimaryDbUser')
    sh_standby = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case025.py start------------')
        self.rootNode = Node()
        self.primary_dbuser = Node(node='PrimaryDbUser')
        self.primary_root = Node(node='PrimaryRoot')
        self.standby1_root = Node(node='Standby1Root')
        self.standby1_dbuser = Node(node='Standby1DbUser')
        self.com = Common()
        self.Constant = Constant()
        self.TPCC_PATH = macro.TPCC_PATH
        self.TPCC_RES = queue.Queue()
        self.pid_file = os.path.join(macro.DB_INSTANCE_PATH, "postmaster.pid")

    def test_startdb(self):
        self.log.info('------------------step2: 删除主机dn1目录---------------------')
        mvcmd = f"mv {macro.DB_INSTANCE_PATH} {macro.DB_INSTANCE_PATH + '.baktest'} "
        self.log.info(mvcmd)
        self.primary_root.sh(mvcmd)

        self.log.info('----------------step3:备机重建 fail ----------------------')
        rebuild_msg = self.sh_standby.executeGsctl('build', self.Constant.BUILD_FAIL_MSG1)
        self.log.info(rebuild_msg)
        self.assertTrue(rebuild_msg)

        status = self.sh_primy.getDbClusterStatus('status')
        self.log.info(status)

        self.log.info('--------------------step4: 恢复主机dn1目录---------------------')
        mvcmd1 = f"mv {macro.DB_INSTANCE_PATH + '.baktest'} {macro.DB_INSTANCE_PATH}"
        self.log.info(mvcmd1)
        self.primary_root.sh(mvcmd1)

        self.log.info('------------------step5: 备机重建 success---------------------')
        rebuild_msg = self.sh_standby.executeGsctl('build', self.Constant.REBUILD_SUCCESS_MSG)
        self.log.info(rebuild_msg)
        self.assertTrue(rebuild_msg)

        time.sleep(5)

        status = self.sh_primy.getDbClusterStatus('status')
        self.log.info(status)

        self.log.info('--------------------step6: kill掉备机---------------------------')
        msg = self.com.kill_pid(self.standby1_root, 9)
        self.assertEqual(msg, '')

        self.log.info('--------------------step7: 删除主机dn1目录------------------------')
        self.log.info(mvcmd)
        self.primary_root.sh(mvcmd)

        self.log.info('-----------------step8:备机重建 fail--------------------')
        rebuild_msg = self.sh_standby.executeGsctl('build', self.Constant.BUILD_FAIL_MSG1)
        self.log.info(rebuild_msg)
        self.assertTrue(rebuild_msg)

        time.sleep(5)

        status = self.sh_primy.getDbClusterStatus('status')
        self.log.info(status)

        self.log.info('-----------------step9:恢复主机dn1目录---------------------------------')
        self.log.info(mvcmd1)
        self.primary_root.sh(mvcmd1)

        self.log.info('-----------------step10:主备重启---------------------------------')
        self.log.info('查询数据库状态')
        status = self.sh_primy.getDbClusterStatus('detail')
        self.log.info(status)
        self.log.info('查询进程信息')
        result = self.com.get_sh_result(self.primary_dbuser,
                                        "ps -ef | grep gaussdb")
        self.log.info(result)
        result = self.com.get_sh_result(self.primary_dbuser,
                                        f"cat {self.pid_file}")
        self.log.info(result)

        result = self.sh_primy.stopDbCluster()
        self.assertTrue(result)

        self.log.info('查询进程信息')
        result = self.com.get_sh_result(self.primary_dbuser,
                                        "ps -ef | grep gaussdb")
        self.log.info(result)
        result = self.com.get_sh_result(self.primary_dbuser,
                                        f"cat {self.pid_file}")
        self.log.info(result)

        result = self.sh_primy.startDbCluster()
        self.assertTrue(result)

        status = self.sh_primy.getDbClusterStatus('status')
        self.log.info(status)

        self.log.info('-----------------step11:删除备机dn1目录----------------------------')
        self.log.info(mvcmd)
        self.standby1_root.sh(mvcmd)

        self.log.info('-----------------step12:备机重建 fail----------------------------')
        rebuild_msg = self.sh_standby.executeGsctl('build', self.Constant.NO_FILE_MSG)
        self.log.info(rebuild_msg)
        self.assertTrue(rebuild_msg)

        time.sleep(5)

        status = self.sh_primy.getDbClusterStatus('status')
        self.log.info(status)

        self.log.info('----------------------------step13:恢复备机dn1目录--------------------------')
        self.log.info(mvcmd1)
        self.standby1_root.sh(mvcmd1)

        self.log.info('----------------------------step14:主备重启-----------------------------------')
        result = self.sh_primy.startDbCluster()
        self.log.info(result)

        status = self.sh_primy.getDbClusterStatus('status')
        self.log.info(status)

        self.log.info('----------------------------step15:kill掉备机-----------------------------')
        msg = self.com.kill_pid(self.standby1_root, 9)
        self.assertEqual(msg, '')

        self.log.info('----------------------------step16:删除备机dn1目录-----------------------------')
        self.log.info(mvcmd)
        self.standby1_root.sh(mvcmd)

        self.log.info('-----------------------------step17:备机重建fail----------------------------')
        rebuild_msg = self.sh_standby.executeGsctl('build', self.Constant.NO_FILE_MSG)
        self.log.info(rebuild_msg)
        self.assertTrue(rebuild_msg)

        time.sleep(5)

        status = self.sh_primy.getDbClusterStatus('status')
        self.log.info(status)

        self.log.info('-----------------------------step18:恢复备机dn1目录---------------------------')
        self.log.info(mvcmd1)
        self.standby1_root.sh(mvcmd1)

        self.log.info('-----------------------------step19:主备重启----------------------')
        result = self.sh_primy.startDbCluster()
        self.assertTrue(result)

        status = self.sh_primy.getDbClusterStatus('status')
        self.log.info(status)
        self.assertTrue(status)

    def tearDown(self):
        self.log.info('-----------------------this is tearDown--------------------------')
        db_status = self.sh_primy.getDbClusterStatus('status')
        if not db_status:
            self.log.info('恢复数据库目录')
            mvcmd1 = f"mv {macro.DB_INSTANCE_PATH + '.baktest'} {macro.DB_INSTANCE_PATH}"
            self.standby1_root.sh(mvcmd1)
            self.primary_root.sh(mvcmd1)
            self.log.info(mvcmd1)
            self.sh_primy.startDbCluster()
        else:
            pass
